package com.pactera.madp.cp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.cp.api.dto.marketing.MarketingExcelCreateDTO;
import com.pactera.madp.cp.api.dto.marketing.MarketingExcelUpdateDTO;
import com.pactera.madp.cp.api.entity.marketing.MarketingExcelEntity;
import com.pactera.madp.cp.api.vo.marketing.MarketingExcelVO;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.mapper.marketing.MarketingExcelMapper;
import com.pactera.madp.cp.service.MarketingExcelService;
import com.pactera.madp.cp.service.MarketingHotService;
import lombok.Data;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (McMarketingExcel)表服务实现类
 *
 * @author yushen.qi
 * @since 2021-05-24 18:56:04
 */
@Service
@Data
public class MarketingExcelServiceImpl extends ServiceImpl<MarketingExcelMapper, MarketingExcelEntity> implements MarketingExcelService {

    @Resource
    private MarketingHotService marketingHotService;

    @Override
    public List<MarketingExcelVO> selectAllOrName(String title, Integer classification) {
        int corpId = CorpContextHolder.getCorpId();
        //判断title是否为空，否为模糊查询
        if(null==title){
            return list(Wrappers.<MarketingExcelEntity>lambdaQuery().eq(MarketingExcelEntity::getCorpId, corpId).eq(MarketingExcelEntity::getClassification,classification)).stream().map(entity ->
            {
                MarketingExcelVO MarketingExcelVO = new MarketingExcelVO();
                BeanUtil.copyProperties(entity, MarketingExcelVO);
                return MarketingExcelVO;
            }).collect(Collectors.toList());
        }
        return list(Wrappers.<MarketingExcelEntity>lambdaQuery().eq(MarketingExcelEntity::getCorpId, corpId).like(MarketingExcelEntity::getTitle,title).eq(MarketingExcelEntity::getClassification,classification)).stream().map(entity ->
        {
            MarketingExcelVO MarketingExcelVO = new MarketingExcelVO();
            BeanUtil.copyProperties(entity, MarketingExcelVO);
            return MarketingExcelVO;
        }).collect(Collectors.toList());
    }

    @Override
    public IPage selectAllByPageOrName(Page page, String title, Integer classification) {
        Integer corpId = CorpContextHolder.getCorpId();
        QueryWrapper<MarketingExcelEntity> wrapper = new QueryWrapper<>();
        if(null==title){
            wrapper.eq("corp_id",corpId).eq("marketing_state",2).eq("classification",classification);
        }else {
            wrapper.like("title",title).eq("marketing_state",2).eq("corp_id",corpId).eq("classification",classification);
        }
        IPage iPage = baseMapper.selectPage(page, wrapper);
        Object collect = iPage.getRecords().stream().map(entity -> {
            MarketingExcelVO marketingExcelVO = new MarketingExcelVO();
            BeanUtil.copyProperties(entity, marketingExcelVO);
            return marketingExcelVO;
        }).collect(Collectors.toList());
        iPage.setRecords((List) collect);
        return iPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Integer> insert(MarketingExcelCreateDTO dto) {
        Integer corpId = CorpContextHolder.getCorpId();
        int tenantId = AccountService.getTenantId();
        MarketingExcelEntity excelEntity = new MarketingExcelEntity();
        BeanUtil.copyProperties(dto,excelEntity);
        //设置新增的excel参数
        excelEntity.setCreateTime(LocalDateTime.now());
        excelEntity.setUpdateTime(LocalDateTime.now());
        excelEntity.setMarketingCover("/mfs/excel.png");
        excelEntity.setCorpId(corpId);
        excelEntity.setHotUuid("a");
        excelEntity.setRecState(2);
       // excelEntity.setTenantId(tenantId);
        excelEntity.setMarketingState(1);
        save(excelEntity);
        MarketingExcelEntity one = getOne(Wrappers.<MarketingExcelEntity>lambdaQuery().eq(MarketingExcelEntity::getCorpId, corpId).orderByDesc(MarketingExcelEntity::getId).last("limit 1"));
        HashMap<String, Integer> hashMap = new HashMap<>(1);
        hashMap.put("id",one.getId());
        return hashMap;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(MarketingExcelUpdateDTO excelDto) {
        MarketingExcelEntity excelEntity = new MarketingExcelEntity();
        BeanUtil.copyProperties(excelDto,excelEntity);
        excelEntity.setUpdateTime(LocalDateTime.now());
        excelEntity.setMarketingState(3);
        excelEntity.setHotUuid(null);
        excelEntity.setRecState(2);
        return update(excelEntity, Wrappers.<MarketingExcelEntity>lambdaQuery().eq(MarketingExcelEntity::getId,excelEntity.getId()));
    }

    @Override
    public R remove(Integer id) {
        MarketingExcelEntity one = getOne(Wrappers.<MarketingExcelEntity>lambdaQuery().eq(MarketingExcelEntity::getId,id));
        if (one.getMarketingState()==2){
            return R.failed("该素材已被发布，无法删除！");
        }
        if(one.getRecState()==1){
            return R.failed("该素材已被推荐，无法删除！");
        }
//        MarketingHotEntity hotEntity = marketingHotService.getOne(Wrappers.<MarketingHotEntity>lambdaQuery().eq(MarketingHotEntity::getCorpId,CorpContextHolder.getCorpId()).orderByDesc(MarketingHotEntity::getId).last("limit 1"));
//        if(null!=hotEntity){
//            if(one.getHotUuid().equals(hotEntity.getHotUuid())){
//                return R.failed("该素材已在热榜发布，无法删除！");
//            }
//        }
        return remove(Wrappers.<MarketingExcelEntity>lambdaQuery().eq(MarketingExcelEntity::getId,id))?R.ok():R.failed("删除失败！");
    }

}
